<?php
include_once 'Database.php';
/**
 * Clase para realizar consultas a la base de datos (CRUD)
 *
 * @author Jose Arcos
 */
class Modelo
{
    
    private $_db;
    
    public function __construct()
    {
        $database = Database::getInstance();
        $this->_db = $database->getConnection();
    }
    
    /**
     * 
     * @param string $table
     * @param array $data
     * @return int
     */
    protected function insert( $table, array $data  )
    {
        $table = (string) $table;
        $sql = '';
        $result = 0;
        if( !empty( $table ) ){
            
            $sql = "INSERT INTO $table " . $this->_dataInsert($data);   
            $result = $this->_db->query($sql) ? $this->_db->insert_id : 0;
            
        }
        return $result;
    }

    /**
     * 
     * @param string $table
     * @param array $where
     * @return type
     */
    protected function read( $table, array $where = array()  )
    {
        $where = $this->_where( $where );
        $sql = "SELECT * FROM $table $where";
        $results = $this->fetchAll( $sql );
        return $results;
    }
    
    /**
     * 
     * @param string $sql
     * @return array
     */
    protected function fetchAll( $sql )
    {
        $sql = (string) $sql;
        $results = array();
        $dataset = $this->_db->query( $sql );
        if ( $dataset ) {
            while( $row = $dataset->fetch_object() ){
                $results[] = $row;
            }
            $dataset->close(); 
        }
        
        return $results;
        
    }
    
    /**
     * 
     * @param string $sql
     * @return object
     */
    protected function fetchRow( $sql )
    {
        $sql = (string) $sql;
        $row = '';
        $dataset = $this->_db->query( $sql );
        if ( $dataset ) {
            $row = $dataset->fetch_object(); 
            $dataset->close(); 
        }
        
        return $row;        
    }
    
    
    protected function query( $sql )
    {
        $sql = (string) $sql;
        return $this->_db->query( $sql );
    }

    /**
     * 
     * @param string $table
     * @param array $data
     * @param array $where
     * @return booleans
     */
    protected function update( $table, array $data, array $where )
    {
        
        $table = (string) $table;
        $sql = '';
        $result = false;
        if( !empty( $table ) ){
            $set = $this->_dataUpdate($data);
            $where = $this->_where($where);
            $sql = "UPDATE $table $set $where";   
            $result = $this->_db->query($sql) ;            
        }
 
        return $result;
    }

    /**
     * 
     * @param string $table
     * @param array $where
     * @return boolean
     */
    protected function delete( $table, array $where  )
    {
        $table = (string) $table;
        $sql = '';
        $result = false;
        if( !empty( $table ) ){
            $where = $this->_where($where);
            $sql = "DELETE FROM $table $where";   
            $result = $this->_db->query($sql) ;            
        }
 
        return $result;
    }

    private function _select( $select  )
    {
        
        
    }
    
    /**
     * 
     * @param array $data
     * @return string
     */
    private function _dataInsert( array $data )
    {        
        $sql = $setString = $valuesString = '';
        $setArray = array();
        $valuesArray = array();  
        if( !empty( $data ) ){
            foreach ( $data as $key => $value ) {
                $valor = is_int($value) ? $value : "'$value'";
                $setArray[] = $key;
                $valuesArray[] = $valor;
            }
            $setString = implode(', ', $setArray);
            $valuesString = implode(', ', $valuesArray);
            $sql = '( ' . $setString . ' ) VALUES ( ' . $valuesString . ' )';
        }
        return $sql;
        
    }
    
    /**
     * 
     * @param array $data
     * @return string
     */
    private function _dataUpdate( array $data )
    {
        $sql = '';
        $dataArray = array();
        if( !empty( $data ) ){
            foreach ( $data as $key => $value ) {
                $valor = is_int( $value ) ? $value : "'$value'";
                $dataArray[] = $key . '=' . $valor;
            }
            $sql = 'SET ' . implode(', ', $dataArray );
        }
        return $sql;
    }

    /**
     * 
     * 
     * @param array $where
     * @return string
     * 
     */
    private function _where( array $where )
    {
        $sql = '';
        $whereArray = array();
        if( !empty( $where ) ){
            foreach ( $where as $key => $value ) {
                $valor = is_int($value) ? $value : "'$value'";
                $whereArray[] = $key . '=' . $valor;
            }
            $sql = 'WHERE ( ' . implode(' ) AND ( ', $whereArray ) . ' );';
        }
        return $sql;
    }
    
    
}
